package cn.hsa.fqz.task.service.impl;

import cn.hsa.fqz.task.service.dto.UserMappingMinDTO;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hsa.fqz.task.entity.TUserMappingDO;
import cn.hsa.fqz.task.service.TUserMappingService;
import cn.hsa.fqz.task.dao.TUserMappingMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TUserMappingServiceImpl extends ServiceImpl<TUserMappingMapper, TUserMappingDO>
        implements TUserMappingService {

    @Override
    public synchronized boolean saveOrUpdateCurrentUser(CurrentUser currentUser) {

        LambdaQueryWrapper<TUserMappingDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TUserMappingDO::getUserAcctId, currentUser.getUserAcctID());
        //TUserMappingDO userMappingDO = this.getOne(wrapper);
        List<TUserMappingDO> userMappingDOS = this.list(wrapper);
        if (CollectionUtil.isEmpty(userMappingDOS)) {
            TUserMappingDO userMappingDO = new TUserMappingDO();
            userMappingDO.setUserAcctId(currentUser.getUserAcctID());
            userMappingDO.setUserAcct(currentUser.getUserAcct());
            userMappingDO.setDeptId(currentUser.getDeptID());
            userMappingDO.setDeptName(currentUser.getDeptName());
            userMappingDO.setOrgCodg(currentUser.getOrgCodg());
            userMappingDO.setOrgName(currentUser.getOrgName());
            userMappingDO.setName(currentUser.getName());
            userMappingDO.setOrgUntId(currentUser.getOrgUntID());
            userMappingDO.setAdmDvs(currentUser.getAdmDvs());
            userMappingDO.setPoolAreaCodg(currentUser.getPoolAreaCodg());
            userMappingDO.setPoolAreaMap(JSON.toJSONString(currentUser.InsuTypePoolAreaMap));
            userMappingDO.setPrntOrgId(currentUser.getPrntOrgID());
            userMappingDO.setUactId(currentUser.getUactID());

            return this.save(userMappingDO);
        } else {
            TUserMappingDO userMappingDO = userMappingDOS.get(0);
            userMappingDO.setUserAcctId(currentUser.getUserAcctID());
            userMappingDO.setUserAcct(currentUser.getUserAcct());
            userMappingDO.setDeptId(currentUser.getDeptID());
            userMappingDO.setDeptName(currentUser.getDeptName());
            userMappingDO.setOrgCodg(currentUser.getOrgCodg());
            userMappingDO.setOrgName(currentUser.getOrgName());
            userMappingDO.setName(currentUser.getName());
            userMappingDO.setOrgUntId(currentUser.getOrgUntID());
            userMappingDO.setAdmDvs(currentUser.getAdmDvs());
            userMappingDO.setPoolAreaCodg(currentUser.getPoolAreaCodg());
            userMappingDO.setPoolAreaMap(JSON.toJSONString(currentUser.InsuTypePoolAreaMap));
            userMappingDO.setPrntOrgId(currentUser.getPrntOrgID());
            userMappingDO.setUactId(currentUser.getUactID());

           return this.updateById(userMappingDO);

        }
    }

    @Override
    public List<TUserMappingDO> getUserMappingByName(String name){
        LambdaQueryWrapper<TUserMappingDO>
                wrapper = new LambdaQueryWrapper<>();
        wrapper.like(TUserMappingDO::getName, name);
        List<TUserMappingDO> list = this.list(wrapper);
        return list;
    }

    @Override
    public List<UserMappingMinDTO> getAllUserMapping() {
        List<TUserMappingDO> list = this.list();
        List<TUserMappingDO> dataList = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TUserMappingDO::getUserAcct))), ArrayList::new));
        List<UserMappingMinDTO> result = Lists.newArrayList();
        for (TUserMappingDO userDO:dataList) {
            UserMappingMinDTO dto = new UserMappingMinDTO();
            dto.setUserAcct(userDO.getUserAcct());
            dto.setName(userDO.getName());
            result.add(dto);
        }
        return result;
    }
}




